<template>
  <div class="sidebar-container">
    <div class="cat-container" :class="{ 'show-cat': isVisible }">
      <div class="rope" :style="{ height: ropeLength + 'px' }"></div>
      <div class="cat">
        <img src="@/assets/images/cat.png" alt="cat">
      </div>
    </div>
    <div class="buttons-container">
      <div class="sidebar-button" :class="{ 'settings-active': showSettings }" @click="handleSettings" data-tooltip="设置">
        <i class="fas fa-cog"></i>
        <div class="settings-drawer" :class="{ 'show-drawer': showSettings }">
        </div>
      </div>
      <div class="sidebar-button" @click="handleShare" data-tooltip="分享链接">
        <i class="fas fa-share-alt"></i>
      </div>
      <div class="sidebar-button" @click="scrollToTop" data-tooltip="回到顶部">
        <div class="percentage">{{ scrollPercentage }}%</div>
      </div>
      <div class="sidebar-button" @click="scrollToBottom" data-tooltip="回到底部">
        <i class="fas fa-arrow-down"></i>
      </div>
    </div>
    <!-- 提示消息 -->
    <div class="mode-message" :class="{ 'show-message': showMessage }">
      {{ modeMessage }}
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'Sidebar',
  data() {
    return {
      scrollPercentage: 0,
      ropeLength: 0,
      headerHeight: 0,
      isVisible: false,
      maxRopeLength: 400,
      minRopeLength: 20,
      showSettings: false,
      showMessage: false,
      modeMessage: ''
    }
  },
  mounted() {
    // 获取实际的header高度
    const header = document.querySelector('.home-header')
    if (header) {
      this.headerHeight = header.offsetHeight
    }
    window.addEventListener('scroll', this.handleScroll)
    this.handleScroll() // 初始化时调用一次
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  },
  methods: {
    handleScroll() {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop
      const scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight
      this.scrollPercentage = Math.round((scrollTop / scrollHeight) * 100) || 0
      
      // 计算绳子长度和显示状态
      if (scrollTop > 0) {
        this.isVisible = true
        
        // 根据滚动百分比计算绳子长度
        if (this.scrollPercentage <= 30) {
          const progress = this.scrollPercentage / 30
          this.ropeLength = this.minRopeLength + (this.maxRopeLength * 0.5 - this.minRopeLength) * progress
        } else {
          const progress = (this.scrollPercentage - 30) / 70
          this.ropeLength = (this.maxRopeLength * 0.5) + (this.maxRopeLength * 0.5) * progress
        }
      } else {
        this.isVisible = false
        this.ropeLength = 0
      }
    },
    handleSettings() {
      this.showSettings = !this.showSettings
    },
    async handleShare() {
      try {
        // 获取当前页面的 URL
        const currentUrl = window.location.href
        
        // 复制到剪贴板
        await navigator.clipboard.writeText(currentUrl)
        
        // 显示成功提示
        ElMessage({
          message: '成功复制分享信息🎉\n您现在可以通过粘贴直接跟小伙伴分享了！',
          type: 'success',
          duration: 3000,
          offset: 50,
          grouping: true,
          customClass: 'share-message'
        })
      } catch (err) {
        // 如果复制失败，显示错误提示
        ElMessage({
          message: '复制失败，请手动复制链接',
          type: 'error',
          duration: 3000,
          offset: 50,
          grouping: true,
          customClass: 'share-message'
        })
      }
    },
    scrollToTop() {
      document.documentElement.scrollTo({
        top: 0,
        behavior: 'smooth'
      })
    },
    scrollToBottom() {
      const scrollHeight = document.documentElement.scrollHeight
      document.documentElement.scrollTo({
        top: scrollHeight,
        behavior: 'smooth'
      })
    }
  }
}
</script>

<style scoped>
.sidebar-container {
  position: fixed;
  right: 20px;
  bottom: 100px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  z-index: 1000;
}

.cat-container {
  position: fixed;
  right: 40px;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  opacity: 0;
  transform: translateY(-100%);
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.cat-container.show-cat {
  opacity: 1;
  transform: translateY(0);
}

.rope {
  width: 2px;
  background: #8B4513;
  transition: height 0.5s ease;
  position: relative;
  animation: swingRope 3s ease-in-out infinite;
  transform-origin: top center;
}

.cat {
  width: 40px;
  height: 40px;
  position: absolute;
  bottom: -40px;
  left: 50%;
  transform: translateX(-50%);
  animation: swingCat 3s ease-in-out infinite;
  transform-origin: top center;
  will-change: transform;
}

.cat img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.buttons-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sidebar-button {
  position: relative;
  width: 40px;
  height: 40px;
  background-color: rgba(38, 166, 154, 0.9);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: white;
  font-size: 18px;
  transition: all 0.3s ease;
  user-select: none;
  backdrop-filter: blur(5px);
}

/* 深色模式下的按钮样式 */
:deep(.dark-mode) .sidebar-button {
  background-color: rgba(255, 152, 0, 0.9);
}

.sidebar-button:hover {
  background-color: rgba(255, 152, 0, 0.9);
}

:deep(.dark-mode) .sidebar-button:hover {
  background-color: rgba(255, 193, 7, 0.9);
}

/* 提示文字样式 */
.sidebar-button::before {
  content: attr(data-tooltip);
  position: absolute;
  right: 120%;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 14px;
  white-space: nowrap;
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
}

:deep(.dark-mode) .sidebar-button::before {
  background-color: rgba(255, 255, 255, 0.15);
}

.percentage {
  font-size: 14px;
  font-weight: bold;
}

.sidebar-button:active {
  transform: scale(0.95);
}

@keyframes swingCat {
  0% {
    transform: translateX(-50%) rotate(-5deg);
  }
  50% {
    transform: translateX(-50%) rotate(5deg);
  }
  100% {
    transform: translateX(-50%) rotate(-5deg);
  }
}

@keyframes swingRope {
  0% {
    transform: rotate(-3deg);
  }
  50% {
    transform: rotate(3deg);
  }
  100% {
    transform: rotate(-3deg);
  }
}

.settings-drawer {
  position: absolute;
  right: 0;
  bottom: 50px;
  display: flex;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.settings-drawer.show-drawer {
  opacity: 1;
  visibility: visible;
}

.settings-drawer::after {
  display: none;
}

.theme-button {
  width: 40px;
  height: 40px;
  background-color: rgba(38, 166, 154, 0.9);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
}

.theme-button:hover {
  background-color: rgba(255, 152, 0, 0.9);
}

:deep(.dark-mode) .theme-button {
  background-color: rgba(255, 152, 0, 0.9);
}

:deep(.dark-mode) .theme-button:hover {
  background-color: rgba(255, 193, 7, 0.9);
}

/* 提示消息样式 */
.mode-message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(-100px);
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  transition: all 0.3s ease;
  opacity: 0;
  z-index: 1001;
}

.mode-message.show-message {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* 当设置抽屉显示时，设置按钮保持橙色 */
.sidebar-button.settings-active {
  background-color: #ff9800;
}

/* 自定义消息样式 */
.custom-message {
  background-color: rgba(0, 0, 0, 0.8) !important;
  color: white !important;
  backdrop-filter: blur(5px) !important;
}

:deep(.dark-mode) .custom-message {
  background-color: rgba(255, 255, 255, 0.15) !important;
}

/* 移除原来的提示消息样式 */
.mode-message {
  display: none;
}

/* 分享提示消息样式 */
:deep(.share-message) {
  left: 20px !important;
  right: auto !important;
  top: 20px !important;
  min-width: 300px !important;
  background-color: rgba(0, 0, 0, 0.8) !important;
  color: white !important;
  backdrop-filter: blur(5px) !important;
  white-space: pre-line !important; /* 允许换行显示 */
}

:deep(.dark-mode .share-message) {
  background-color: rgba(255, 255, 255, 0.15) !important;
}
</style>
